package com.amgl.mobike.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;

import java.util.Iterator;
import java.util.List;

/**
 * Created by 阿木 on 2017/5/27.
 */

public class CommonUtils {
    /**
     * 是否为应用主进程
     *
     * @param context
     * @return
     */
    public static boolean isMainProcess(Context context) {
        String processName = getAppName(context);
        return (!TextUtils.isEmpty(processName) && processName.equalsIgnoreCase(context.getPackageName()));
    }

    /**
     * 获取ProcessName
     *
     * @param context
     * @return
     */
    public static String getAppName(Context context) {
        if (context == null)
            return "";
        int pID = android.os.Process.myPid();
        String processName = null;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pID) {
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return processName;
    }
}
